import zipfile
from zipfile import ZipFile

from dataclasses import dataclass, field
import os
import re
#第一个问题，save-trace文件的读取和解析
"""
根据生成的save-trace.zip中tarce.trace文件 "type":"action"
{
    "type": "action",
    "metadata": {
        "id": "call@7",
        "stack": [],
        "apiName": "",
        "internal": true,
        "objectId": "browser-context@a25f267a5efddf1c6929e7ba31754831",
        "wallTime": 1640853111038,
        "startTime": 278984753.167,
        "endTime": 278991496.283,
        "type": "BrowserContext",
        "method": "recorderSupplementEnable",
        "params": {
            "language": "python",
            "startRecording": true,
            "launchOptions": {
                "channel": "chrome"
            },
            "contextOptions": {
                "viewport": {
                    "width": 1280,
                    "height": 720
                },
                "geolocation": {
                    "latitude": 37.819722,
                    "longitude": -122.478611
                },
                "permissions": [
                    "geolocation"
                ],
                "locale": "zh-CN",
                "colorScheme": "light",
                "timezoneId": "Asia/Shanghai"
            },
            "saveStorage": "saveStorage.json",
            "outputFile": "D:\\OdooPlaywrightStreamlit\\playwright_streamlit\\playwright\\record_script\\NewCreatePython.py"
        },
        "log": [],
        "snapshots": [],
        "afterSnapshot": "after@call@7"
    }
}

{
    "type": "action",
    "metadata": {
        "id": "call@d6e5bb9c780907e826b3d7dd9377d1e6",
        "apiName": "frame.click",
        "objectId": "frame@5862e241187d1f285610959f8e1592b5",
        "pageId": "page@a3715be3efc26b7c7c606a98cd71dee1",
        "frameId": "frame@5862e241187d1f285610959f8e1592b5",
        "wallTime": 1640853126465,
        "startTime": 279000180.598,
        "endTime": 279000542.45,
        "type": "Frame",
        "method": "click",
        "params": {
            "selector": "text=你好，请登录"
        },
        "log": [
            "waiting for selector \"text=你好，请登录\"",
            "  selector resolved to visible <a class=\"link-login\" href=\"javascript:login();\">你好，请登录</a>",
            "attempting click action",
            "  waiting for element to be visible, enabled and stable",
            "  element is visible, enabled and stable",
            "  scrolling into view if needed",
            "  done scrolling",
            "  checking that element receives pointer events at (384.48,81.48)",
            "  element does receive pointer events",
            "  performing click action",
            "  click action done",
            "  waiting for scheduled navigations to finish",
            "  navigated to \"https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F\"",
            "  navigations have finished"
        ],
        "snapshots": [
            {
                "title": "before",
                "snapshotName": "before@call@d6e5bb9c780907e826b3d7dd9377d1e6"
            },
            {
                "title": "action",
                "snapshotName": "action@call@d6e5bb9c780907e826b3d7dd9377d1e6"
            },
            {
                "title": "after",
                "snapshotName": "after@call@d6e5bb9c780907e826b3d7dd9377d1e6"
            }
        ],
        "afterSnapshot": "after@call@d6e5bb9c780907e826b3d7dd9377d1e6",
        "point": {
            "x": 384.48,
            "y": 81.48
        }
    }
}

{
    "type": "action",
    "metadata": {
        "id": "call@8daffbf37e1994bac9580dc39fd8b7f7",
        "apiName": "frame.click",
        "objectId": "frame@5862e241187d1f285610959f8e1592b5",
        "pageId": "page@a3715be3efc26b7c7c606a98cd71dee1",
        "frameId": "frame@5862e241187d1f285610959f8e1592b5",
        "wallTime": 1640853133048,
        "startTime": 279006763.794,
        "endTime": 279006789.119,
        "type": "Frame",
        "method": "click",
        "params": {
            "selector": "[placeholder=\"邮箱/用户名/登录手机\"]"
        },
        "log": [
            "waiting for selector \"[placeholder=\"邮箱/用户名/登录手机\"]\"",
            "  selector resolved to visible <input value=\"\" type=\"text\" class=\"itxt\" tabindex=\"1\" _…/>",
            "attempting click action",
            "  waiting for element to be visible, enabled and stable",
            "  element is visible, enabled and stable",
            "  scrolling into view if needed",
            "  done scrolling",
            "  checking that element receives pointer events at (962,275.78)",
            "  element does receive pointer events",
            "  performing click action",
            "  click action done",
            "  waiting for scheduled navigations to finish",
            "  navigations have finished"
        ],
        "snapshots": [
            {
                "title": "before",
                "snapshotName": "before@call@8daffbf37e1994bac9580dc39fd8b7f7"
            },
            {
                "title": "action",
                "snapshotName": "action@call@8daffbf37e1994bac9580dc39fd8b7f7"
            },
            {
                "title": "after",
                "snapshotName": "after@call@8daffbf37e1994bac9580dc39fd8b7f7"
            }
        ],
        "afterSnapshot": "after@call@8daffbf37e1994bac9580dc39fd8b7f7",
        "point": {
            "x": 962,
            "y": 275.78
        }
    }
}
"""



@dataclass(order=True)  # 注意这里
class CreateRunFile(object):
    url: str = field(default="https://www.baidu.com")
    output: str = field(default="NewCreatePython.py")
    target: str = field(default=" python")

    def read_and_parse(self):
        '''
        # 获取zip文件中的文件名
        '''
        path = "save-trace.zip"
        # # 定义一个文件名列表
        # names = []
        # # 读取文件
        # __Z = zipfile.ZipFile(path)
        #
        # # 遍历文件对象
        # for name in __Z.NameToInfo:
        #     if name.cout('/') == 3 and not name.endswith('/'):
        #         names.append(name)
        # return names
        # zfile = zipfile.ZipFile("save-trace.zip", "r")
        # zfile.extractall()
        # with ZipFile(path, 'r') as myzip:
        #     print(myzip.getinfo('trace.trace'))
        #     print(myzip.open('trace.trace','r'))
            # print(myzip.infolist())
            # print(myzip.namelist())
        with ZipFile(path) as myzip:
            with myzip.open('trace.trace') as myfile:
                re_s = str(myfile.read())
                re_list = re.findall(r"{\"(.*?)\\n" , re_s)
                print(re_list[0])
                print(re_list[1])
                print(re_list[2])

if __name__ == '__main__':
    createRunFile = CreateRunFile()
    createRunFile.read_and_parse()